<template>
  <div class="app-container">
    <el-card>
      <el-row :gutter="20" align="middle">
        <el-col :span="24" class="input-button-group">
          <el-select v-model="communityId" placeholder="选择小区名称" clearable @change="fetchBuildingList">
            <el-option
              v-for="community in communityList"
              :key="community.id"
              :label="community.name"
              :value="community.id"
            />
          </el-select>

          <el-select v-model="buildingId" placeholder="请选择楼栋" clearable>
            <el-option
              v-for="item in buildingList"
              :key="item.id"
              :label="item.number"
              :value="item.id"
            />
          </el-select>

          <el-select v-model="statusId" placeholder="选择状态" clearable>
            <el-option :value="7" label="已处理" />
            <el-option :value="8" label="未处理" />
          </el-select>

          <el-button icon="el-icon-search" @click="handleSearch">筛选</el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      stripe
      height="532"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="100" sortable :sort-by="sortById">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>

      <el-table-column label="所属小区" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.communityName }}
        </template>
      </el-table-column>

      <el-table-column label="报修图片" width="150" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.image" alt="车辆照片" style="width: 100px; height: auto;">
        </template>
      </el-table-column>

      <el-table-column label="楼栋编号" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.buildingNumber }}
        </template>
      </el-table-column>

      <el-table-column label="描述" width="170" align="center">
        <template slot-scope="scope">
          {{ scope.row.description }}
        </template>
      </el-table-column>

      <el-table-column label="报修状态" width="197" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.statusId"
            :active-color="'#13ce66'"
            :inactive-color="'#ff4949'"
            :active-value="7"
            :inactive-value="8"
            active-text="已处理"
            inactive-text="未处理"
            @change="handleStatusChange(scope.row)"
          />
        </template>
      </el-table-column>

      <el-table-column label="报修日期" width="170" align="center">
        <template slot-scope="scope">
          {{ scope.row.createDate }}
        </template>
      </el-table-column>

      <!-- Actions Column -->
      <el-table-column label="操作" width="190" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页 -->
    <div class="block" style="text-align: center;">
      <el-pagination
        :current-page="page"
        :page-sizes="[4, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { getList, deleteRow, updateRow } from '@/api/repair'
import { getCommunityList } from '@/api/community'
import { getBuildingList } from '@/api/building'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      page: 1,
      pageSize: 4,
      communityId: '',
      buildingId: '',
      statusId: '',
      total: 0,
      communityList: [],
      buildingList: []
    }
  },
  created() {
    this.fetchData()
  },
  mounted() {
    this.fetchCommunityList()
  },
  methods: {
    fetchBuildingList() {
      getBuildingList({ communityId: this.communityId }).then(response => {
        this.buildingList = response.data
        console.log(response.data)
      })
    },
    fetchCommunityList() {
      getCommunityList().then(response => {
        this.communityList = response.data
        console.log(response.data)
      })
    },
    fetchData() {
      this.listLoading = true
      getList({ page: this.page, pageSize: this.pageSize, communityId: this.communityId, buildingId: this.buildingId, statusId: this.statusId })
        .then(response => {
          this.list = response.data.records
          this.total = response.data.total
          this.listLoading = false
        })
    },
    sortById(a, b) {
      return a.id - b.id
    },
    handleStatusChange(row) {
      const updateData = {
        id: row.id,
        statusId: row.statusId
      }
      updateRow(updateData)
        .then(() => {
          this.$message.success('状态更新成功')
        })
    },
    handleSearch() {
      this.page = 1
      this.fetchData()
    },
    handleSizeChange(size) {
      this.pageSize = size
      this.fetchData()
    },
    handleCurrentChange(page) {
      this.page = page
      this.fetchData()
    },
    handleDelete(rowId) {
      const ids = [rowId]
      this.$confirm('确定删除该记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRow(ids).then(() => {
          this.fetchData()
          this.$message.success('删除成功')
        })
      }).catch(() => {
        this.$message('已取消删除')
      })
    }
  }
}
</script>

<style scoped>
.input-button-group {
  display: flex;
  align-items: center;
}

.input-button-group .el-input {
  flex: 1;
  margin-right: 10px;
}
</style>
